package com.usian.controller;

import com.usian.appointments.Appointments;

import com.usian.image.Images;
import com.usian.service.ImagesService;

import com.usian.utils.AliyunOSSUtils;

import com.usian.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;

import java.util.List;


@RestController
@RequestMapping("/app/appointments/images")
public class ImagesController {
    @Autowired
    private ImagesService imagesService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;




    @PostMapping("/selectByImages")
    public List<Appointments> getImages(){
        return imagesService.getImages();
    }

//    @PostMapping("/upload")
//    public ResultVo upload(@RequestParam("image") MultipartFile file
//            ) throws IOException {
//        // 获取文件名
//        String filename = file.getOriginalFilename();
//        // 获取io流
//        InputStream inputStream = file.getInputStream();
//        // 文件上传
//        String fileUtils = aliyunOSSUtils.putObject(inputStream, filename);
//        return ResultVo.ok(fileUtils);
//    }

    @PostMapping("/upload")
    public ResultVo upload(@RequestParam("Image") MultipartFile file,
                           @RequestParam("userId") Integer userId,
                           @RequestParam("appointmentsId") Integer appointmentsId) throws IOException {
        // 获取文件名
        String filename = file.getOriginalFilename();
        // 获取io流
        InputStream inputStream = file.getInputStream();
        // 文件上传
        String imageUrl = aliyunOSSUtils.putObject(inputStream, filename);

        // 创建 Images 对象
        Images images = new Images();
        images.setUserId(userId);
        images.setAppointmentsId(appointmentsId);
        images.setImage(imageUrl);

        // 保存到数据库
        imagesService.addImages(images);

        return ResultVo.ok(imageUrl);
    }


}
